 %Create a class named synapse, placed in file 'synapse.m'
classdef synapse
    properties
        weight;
        definedRechargingTime;  % property of the synapse - defined recharging time
        remainingTime;          % variable that contains remaining time to be active
        active;                 % 1 - active, 0 - inactive
        inputNeuronId;
        outputNeuronId;
    end
    methods
        % Basic function
        function obj = synapse(weight, definedRechargingTime, inputNeuronId, outputNeuronId)
            obj.weight = weight;
            obj.definedRechargingTime = definedRechargingTime;
            obj.remainingTime = 0;      %when we create synapse it by default is active
            obj.active = 1;
            obj.inputNeuronId = inputNeuronId;
            obj.outputNeuronId = outputNeuronId;
        end
        function disp(obj)
            s = sprintf('%d -> %d (weigth: %.2f recharging time: %d active: %d)', ...
                obj.inputNeuronId, obj.outputNeuronId, obj.weight,...
                obj.rechargingTime, obj.active);
            disp(s);
        end
        
        % Functions
        function obj = UpdateWeight(obj, newWeight)
            obj.weight = newWeight;
        end
        function obj = Activate(obj)
            obj.active = 1;
        end
        function obj = Deactivate(obj)
            obj.active = 0;
        end
        function obj = DeactivateWithSettingRemainingTime(obj)
            obj.active = 0;
            obj.remainingTime = obj.definedRechargingTime;
        end
        function obj = DecreaseRemainingTime(obj)
            if (obj.active == 0) && (obj.remainingTime ~= 0)
                obj.remainingTime = obj.remainingTime -1;
                if (obj.remainingTime == 0)
                    obj.active = 1;
                end    
            elseif (obj.active == 1) && (obj.remainingTime ~= 0)
                display('Error! Synapse is active and its remaining time is non-zero');
            elseif (obj.active == 0) && (obj.remainingTime == 0)
                display('Error! Synapse is inactive and its remaining time is zero');
            end
        end
        function ifActive = CheckActiveState(obj)
            if (obj.active == 1) && (obj.remainingTime == 0)
                ifActive = 1;
            else
                ifActive = 0;
            end
        end
        function out = PassSignal(obj, in)
            if(obj.active == 0) || (obj.remainingTime > 0)
                out = 0;
            else
                out = in*obj.weight;
            end
        end
    end
end